# ...existing code...
import tkinter as tk
from tkinter import messagebox
from tkinter import filedialog
from PIL import Image, ImageTk, ImageOps


class RegisterFrame(tk.Frame):
    def __init__(self, parent, controller):
        super().__init__(parent)
        self.controller = controller
        self.db = controller.db

        # 页面标题
        label = tk.Label(self, text="注册", font=("Arial", 16))
        label.pack(pady=20)

        # 头像选择
        self.avatar_path = None
        self.avatar_img = None
        self.avatar_label = tk.Label(self, text="点击选择头像", width=20, height=8, bg="#ddd")
        self.avatar_label.pack(pady=10)
        self.avatar_label.bind("<Button-1>", self.choose_avatar)

        # 用户名输入
        tk.Label(self, text="用户名").pack()
        self.username_entry = tk.Entry(self)
        self.username_entry.pack(pady=5)

        # 手机号输入
        tk.Label(self, text="手机号").pack()
        self.phonenumber_entry = tk.Entry(self)
        self.phonenumber_entry.pack(pady=5)

        # 账号输入
        tk.Label(self, text="账号").pack()
        self.account_entry = tk.Entry(self)
        self.account_entry.pack(pady=5)

        # 密码输入
        tk.Label(self, text="密码").pack()
        self.password_entry = tk.Entry(self, show="*")
        self.password_entry.pack(pady=5)

        # 确认密码
        tk.Label(self, text="确认密码").pack()
        self.confirm_password_entry = tk.Entry(self, show="*")
        self.confirm_password_entry.pack(pady=5)

        self.container = tk.Frame(self)
        self.container.pack(pady=20)

        # 注册按钮
        register_btn = tk.Button(self.container, text="注册", command=self.register)
        register_btn.pack(side="left",padx=20)

        # 返回登录按钮
        back_btn = tk.Button(self.container, text="返回登录", command=lambda: controller.show_frame("LoginFrame"))
        back_btn.pack(side="left", padx=20)

    def choose_avatar(self, event=None):
        file_path = filedialog.askopenfilename(
            title="选择头像",
            filetypes=[("图片文件", "*.png;*.jpg;*.jpeg;*.gif;*.bmp")]
        )
        if file_path:
            self.avatar_path = file_path
            img = Image.open(file_path)
            img = ImageOps.exif_transpose(img)
            img = img.resize((100, 100))
            self.avatar_img = ImageTk.PhotoImage(img)
            self.avatar_label.config(image=self.avatar_img, text="",width=100,height=100,relief="solid")

    def register(self):
        account = self.account_entry.get().strip()
        password = self.password_entry.get()
        confirm_password = self.confirm_password_entry.get()
        username = self.username_entry.get().strip()
        phonenumber = self.phonenumber_entry.get().strip()
        avatar_path = self.avatar_path

        if not account:
            messagebox.showerror("错误", "账号不能为空！")
            return
        
        if not password:
            messagebox.showerror("错误", "密码不能为空！")
            return
        
        if password != confirm_password:
            messagebox.showerror("错误", "两次输入的密码不一致！")
            return

        if not username:
            messagebox.showerror("错误", "用户名不能为空！")
            return
        
        if not phonenumber:
            messagebox.showerror("错误", "手机号不能为空！")
            return
        
        if self.db.is_account_exist(phonenumber):
            messagebox.showerror("错误", "当前账号已注册！")
            return
        
        if self.db.is_phonenumber_exist(phonenumber):
            messagebox.showerror("错误", "当前手机号已注册！")
            return
        
        # 这里将所有信息传递给数据库
        if self.db.register(account, password, username, phonenumber, avatar_path):
            messagebox.showinfo("成功", "注册成功，请登录！")
            self.controller.show_frame("LoginFrame")
        else:
            messagebox.showerror("错误", "该账号已存在！")